// Licensed to Elasticsearch B.V under one or more agreements.
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information.
//
// ███╗   ██╗ ██████╗ ████████╗██╗ ██████╗███████╗
// ████╗  ██║██╔═══██╗╚══██╔══╝██║██╔════╝██╔════╝
// ██╔██╗ ██║██║   ██║   ██║   ██║██║     █████╗
// ██║╚██╗██║██║   ██║   ██║   ██║██║     ██╔══╝
// ██║ ╚████║╚██████╔╝   ██║   ██║╚██████╗███████╗
// ╚═╝  ╚═══╝ ╚═════╝    ╚═╝   ╚═╝ ╚═════╝╚══════╝
// ------------------------------------------------
//
// This file is automatically generated.
// Please do not edit these files manually.
//
// ------------------------------------------------

#nullable restore

using Elastic.Clients.Elasticsearch.Serverless.Fluent;
using Elastic.Clients.Elasticsearch.Serverless.Serialization;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace Elastic.Clients.Elasticsearch.Serverless.Ml;

[JsonConverter(typeof(InferenceConfigCreateConverter))]
/// <summary>
/// <para>Inference configuration provided when storing the model config</para>
/// </summary>
public sealed partial class InferenceConfigCreate
{
	internal InferenceConfigCreate(string variantName, object variant)
	{
		if (variantName is null)
			throw new ArgumentNullException(nameof(variantName));
		if (variant is null)
			throw new ArgumentNullException(nameof(variant));
		if (string.IsNullOrWhiteSpace(variantName))
			throw new ArgumentException("Variant name must not be empty or whitespace.");
		VariantName = variantName;
		Variant = variant;
	}

	internal object Variant { get; }
	internal string VariantName { get; }

	public static InferenceConfigCreate Classification(Elastic.Clients.Elasticsearch.Serverless.Ml.ClassificationInferenceOptions classificationInferenceOptions) => new InferenceConfigCreate("classification", classificationInferenceOptions);
	public static InferenceConfigCreate FillMask(Elastic.Clients.Elasticsearch.Serverless.Ml.FillMaskInferenceOptions fillMaskInferenceOptions) => new InferenceConfigCreate("fill_mask", fillMaskInferenceOptions);
	public static InferenceConfigCreate Ner(Elastic.Clients.Elasticsearch.Serverless.Ml.NerInferenceOptions nerInferenceOptions) => new InferenceConfigCreate("ner", nerInferenceOptions);
	public static InferenceConfigCreate PassThrough(Elastic.Clients.Elasticsearch.Serverless.Ml.PassThroughInferenceOptions passThroughInferenceOptions) => new InferenceConfigCreate("pass_through", passThroughInferenceOptions);
	public static InferenceConfigCreate QuestionAnswering(Elastic.Clients.Elasticsearch.Serverless.Ml.QuestionAnsweringInferenceOptions questionAnsweringInferenceOptions) => new InferenceConfigCreate("question_answering", questionAnsweringInferenceOptions);
	public static InferenceConfigCreate Regression(Elastic.Clients.Elasticsearch.Serverless.Ml.RegressionInferenceOptions regressionInferenceOptions) => new InferenceConfigCreate("regression", regressionInferenceOptions);
	public static InferenceConfigCreate TextClassification(Elastic.Clients.Elasticsearch.Serverless.Ml.TextClassificationInferenceOptions textClassificationInferenceOptions) => new InferenceConfigCreate("text_classification", textClassificationInferenceOptions);
	public static InferenceConfigCreate TextEmbedding(Elastic.Clients.Elasticsearch.Serverless.Ml.TextEmbeddingInferenceOptions textEmbeddingInferenceOptions) => new InferenceConfigCreate("text_embedding", textEmbeddingInferenceOptions);
	public static InferenceConfigCreate TextExpansion(Elastic.Clients.Elasticsearch.Serverless.Ml.TextExpansionInferenceOptions textExpansionInferenceOptions) => new InferenceConfigCreate("text_expansion", textExpansionInferenceOptions);
	public static InferenceConfigCreate ZeroShotClassification(Elastic.Clients.Elasticsearch.Serverless.Ml.ZeroShotClassificationInferenceOptions zeroShotClassificationInferenceOptions) => new InferenceConfigCreate("zero_shot_classification", zeroShotClassificationInferenceOptions);
}

internal sealed partial class InferenceConfigCreateConverter : JsonConverter<InferenceConfigCreate>
{
	public override InferenceConfigCreate Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
	{
		if (reader.TokenType != JsonTokenType.StartObject)
		{
			throw new JsonException("Expected start token.");
		}

		reader.Read();
		if (reader.TokenType != JsonTokenType.PropertyName)
		{
			throw new JsonException("Expected a property name token representing the variant held within this container.");
		}

		var propertyName = reader.GetString();
		reader.Read();
		if (propertyName == "classification")
		{
			var variant = JsonSerializer.Deserialize<Elastic.Clients.Elasticsearch.Serverless.Ml.ClassificationInferenceOptions?>(ref reader, options);
			reader.Read();
			return new InferenceConfigCreate(propertyName, variant);
		}

		if (propertyName == "fill_mask")
		{
			var variant = JsonSerializer.Deserialize<Elastic.Clients.Elasticsearch.Serverless.Ml.FillMaskInferenceOptions?>(ref reader, options);
			reader.Read();
			return new InferenceConfigCreate(propertyName, variant);
		}

		if (propertyName == "ner")
		{
			var variant = JsonSerializer.Deserialize<Elastic.Clients.Elasticsearch.Serverless.Ml.NerInferenceOptions?>(ref reader, options);
			reader.Read();
			return new InferenceConfigCreate(propertyName, variant);
		}

		if (propertyName == "pass_through")
		{
			var variant = JsonSerializer.Deserialize<Elastic.Clients.Elasticsearch.Serverless.Ml.PassThroughInferenceOptions?>(ref reader, options);
			reader.Read();
			return new InferenceConfigCreate(propertyName, variant);
		}

		if (propertyName == "question_answering")
		{
			var variant = JsonSerializer.Deserialize<Elastic.Clients.Elasticsearch.Serverless.Ml.QuestionAnsweringInferenceOptions?>(ref reader, options);
			reader.Read();
			return new InferenceConfigCreate(propertyName, variant);
		}

		if (propertyName == "regression")
		{
			var variant = JsonSerializer.Deserialize<Elastic.Clients.Elasticsearch.Serverless.Ml.RegressionInferenceOptions?>(ref reader, options);
			reader.Read();
			return new InferenceConfigCreate(propertyName, variant);
		}

		if (propertyName == "text_classification")
		{
			var variant = JsonSerializer.Deserialize<Elastic.Clients.Elasticsearch.Serverless.Ml.TextClassificationInferenceOptions?>(ref reader, options);
			reader.Read();
			return new InferenceConfigCreate(propertyName, variant);
		}

		if (propertyName == "text_embedding")
		{
			var variant = JsonSerializer.Deserialize<Elastic.Clients.Elasticsearch.Serverless.Ml.TextEmbeddingInferenceOptions?>(ref reader, options);
			reader.Read();
			return new InferenceConfigCreate(propertyName, variant);
		}

		if (propertyName == "text_expansion")
		{
			var variant = JsonSerializer.Deserialize<Elastic.Clients.Elasticsearch.Serverless.Ml.TextExpansionInferenceOptions?>(ref reader, options);
			reader.Read();
			return new InferenceConfigCreate(propertyName, variant);
		}

		if (propertyName == "zero_shot_classification")
		{
			var variant = JsonSerializer.Deserialize<Elastic.Clients.Elasticsearch.Serverless.Ml.ZeroShotClassificationInferenceOptions?>(ref reader, options);
			reader.Read();
			return new InferenceConfigCreate(propertyName, variant);
		}

		throw new JsonException();
	}

	public override void Write(Utf8JsonWriter writer, InferenceConfigCreate value, JsonSerializerOptions options)
	{
		writer.WriteStartObject();
		if (value.VariantName is not null & value.Variant is not null)
		{
			writer.WritePropertyName(value.VariantName);
			switch (value.VariantName)
			{
				case "classification":
					JsonSerializer.Serialize<Elastic.Clients.Elasticsearch.Serverless.Ml.ClassificationInferenceOptions>(writer, (Elastic.Clients.Elasticsearch.Serverless.Ml.ClassificationInferenceOptions)value.Variant, options);
					break;
				case "fill_mask":
					JsonSerializer.Serialize<Elastic.Clients.Elasticsearch.Serverless.Ml.FillMaskInferenceOptions>(writer, (Elastic.Clients.Elasticsearch.Serverless.Ml.FillMaskInferenceOptions)value.Variant, options);
					break;
				case "ner":
					JsonSerializer.Serialize<Elastic.Clients.Elasticsearch.Serverless.Ml.NerInferenceOptions>(writer, (Elastic.Clients.Elasticsearch.Serverless.Ml.NerInferenceOptions)value.Variant, options);
					break;
				case "pass_through":
					JsonSerializer.Serialize<Elastic.Clients.Elasticsearch.Serverless.Ml.PassThroughInferenceOptions>(writer, (Elastic.Clients.Elasticsearch.Serverless.Ml.PassThroughInferenceOptions)value.Variant, options);
					break;
				case "question_answering":
					JsonSerializer.Serialize<Elastic.Clients.Elasticsearch.Serverless.Ml.QuestionAnsweringInferenceOptions>(writer, (Elastic.Clients.Elasticsearch.Serverless.Ml.QuestionAnsweringInferenceOptions)value.Variant, options);
					break;
				case "regression":
					JsonSerializer.Serialize<Elastic.Clients.Elasticsearch.Serverless.Ml.RegressionInferenceOptions>(writer, (Elastic.Clients.Elasticsearch.Serverless.Ml.RegressionInferenceOptions)value.Variant, options);
					break;
				case "text_classification":
					JsonSerializer.Serialize<Elastic.Clients.Elasticsearch.Serverless.Ml.TextClassificationInferenceOptions>(writer, (Elastic.Clients.Elasticsearch.Serverless.Ml.TextClassificationInferenceOptions)value.Variant, options);
					break;
				case "text_embedding":
					JsonSerializer.Serialize<Elastic.Clients.Elasticsearch.Serverless.Ml.TextEmbeddingInferenceOptions>(writer, (Elastic.Clients.Elasticsearch.Serverless.Ml.TextEmbeddingInferenceOptions)value.Variant, options);
					break;
				case "text_expansion":
					JsonSerializer.Serialize<Elastic.Clients.Elasticsearch.Serverless.Ml.TextExpansionInferenceOptions>(writer, (Elastic.Clients.Elasticsearch.Serverless.Ml.TextExpansionInferenceOptions)value.Variant, options);
					break;
				case "zero_shot_classification":
					JsonSerializer.Serialize<Elastic.Clients.Elasticsearch.Serverless.Ml.ZeroShotClassificationInferenceOptions>(writer, (Elastic.Clients.Elasticsearch.Serverless.Ml.ZeroShotClassificationInferenceOptions)value.Variant, options);
					break;
			}
		}

		writer.WriteEndObject();
	}
}

public sealed partial class InferenceConfigCreateDescriptor<TDocument> : SerializableDescriptor<InferenceConfigCreateDescriptor<TDocument>>
{
	internal InferenceConfigCreateDescriptor(Action<InferenceConfigCreateDescriptor<TDocument>> configure) => configure.Invoke(this);

	public InferenceConfigCreateDescriptor() : base()
	{
	}

	private bool ContainsVariant { get; set; }
	private string ContainedVariantName { get; set; }
	private object Variant { get; set; }
	private Descriptor Descriptor { get; set; }

	private InferenceConfigCreateDescriptor<TDocument> Set<T>(Action<T> descriptorAction, string variantName) where T : Descriptor
	{
		ContainedVariantName = variantName;
		ContainsVariant = true;
		var descriptor = (T)Activator.CreateInstance(typeof(T), true);
		descriptorAction?.Invoke(descriptor);
		Descriptor = descriptor;
		return Self;
	}

	private InferenceConfigCreateDescriptor<TDocument> Set(object variant, string variantName)
	{
		Variant = variant;
		ContainedVariantName = variantName;
		ContainsVariant = true;
		return Self;
	}

	public InferenceConfigCreateDescriptor<TDocument> Classification(ClassificationInferenceOptions classificationInferenceOptions) => Set(classificationInferenceOptions, "classification");
	public InferenceConfigCreateDescriptor<TDocument> Classification(Action<ClassificationInferenceOptionsDescriptor> configure) => Set(configure, "classification");
	public InferenceConfigCreateDescriptor<TDocument> FillMask(FillMaskInferenceOptions fillMaskInferenceOptions) => Set(fillMaskInferenceOptions, "fill_mask");
	public InferenceConfigCreateDescriptor<TDocument> FillMask(Action<FillMaskInferenceOptionsDescriptor> configure) => Set(configure, "fill_mask");
	public InferenceConfigCreateDescriptor<TDocument> Ner(NerInferenceOptions nerInferenceOptions) => Set(nerInferenceOptions, "ner");
	public InferenceConfigCreateDescriptor<TDocument> Ner(Action<NerInferenceOptionsDescriptor> configure) => Set(configure, "ner");
	public InferenceConfigCreateDescriptor<TDocument> PassThrough(PassThroughInferenceOptions passThroughInferenceOptions) => Set(passThroughInferenceOptions, "pass_through");
	public InferenceConfigCreateDescriptor<TDocument> PassThrough(Action<PassThroughInferenceOptionsDescriptor> configure) => Set(configure, "pass_through");
	public InferenceConfigCreateDescriptor<TDocument> QuestionAnswering(QuestionAnsweringInferenceOptions questionAnsweringInferenceOptions) => Set(questionAnsweringInferenceOptions, "question_answering");
	public InferenceConfigCreateDescriptor<TDocument> QuestionAnswering(Action<QuestionAnsweringInferenceOptionsDescriptor> configure) => Set(configure, "question_answering");
	public InferenceConfigCreateDescriptor<TDocument> Regression(RegressionInferenceOptions regressionInferenceOptions) => Set(regressionInferenceOptions, "regression");
	public InferenceConfigCreateDescriptor<TDocument> Regression(Action<RegressionInferenceOptionsDescriptor<TDocument>> configure) => Set(configure, "regression");
	public InferenceConfigCreateDescriptor<TDocument> TextClassification(TextClassificationInferenceOptions textClassificationInferenceOptions) => Set(textClassificationInferenceOptions, "text_classification");
	public InferenceConfigCreateDescriptor<TDocument> TextClassification(Action<TextClassificationInferenceOptionsDescriptor> configure) => Set(configure, "text_classification");
	public InferenceConfigCreateDescriptor<TDocument> TextEmbedding(TextEmbeddingInferenceOptions textEmbeddingInferenceOptions) => Set(textEmbeddingInferenceOptions, "text_embedding");
	public InferenceConfigCreateDescriptor<TDocument> TextEmbedding(Action<TextEmbeddingInferenceOptionsDescriptor> configure) => Set(configure, "text_embedding");
	public InferenceConfigCreateDescriptor<TDocument> TextExpansion(TextExpansionInferenceOptions textExpansionInferenceOptions) => Set(textExpansionInferenceOptions, "text_expansion");
	public InferenceConfigCreateDescriptor<TDocument> TextExpansion(Action<TextExpansionInferenceOptionsDescriptor> configure) => Set(configure, "text_expansion");
	public InferenceConfigCreateDescriptor<TDocument> ZeroShotClassification(ZeroShotClassificationInferenceOptions zeroShotClassificationInferenceOptions) => Set(zeroShotClassificationInferenceOptions, "zero_shot_classification");
	public InferenceConfigCreateDescriptor<TDocument> ZeroShotClassification(Action<ZeroShotClassificationInferenceOptionsDescriptor> configure) => Set(configure, "zero_shot_classification");

	protected override void Serialize(Utf8JsonWriter writer, JsonSerializerOptions options, IElasticsearchClientSettings settings)
	{
		if (!ContainsVariant)
		{
			writer.WriteNullValue();
			return;
		}

		writer.WriteStartObject();
		writer.WritePropertyName(ContainedVariantName);
		if (Variant is not null)
		{
			JsonSerializer.Serialize(writer, Variant, Variant.GetType(), options);
			writer.WriteEndObject();
			return;
		}

		JsonSerializer.Serialize(writer, Descriptor, Descriptor.GetType(), options);
		writer.WriteEndObject();
	}
}

public sealed partial class InferenceConfigCreateDescriptor : SerializableDescriptor<InferenceConfigCreateDescriptor>
{
	internal InferenceConfigCreateDescriptor(Action<InferenceConfigCreateDescriptor> configure) => configure.Invoke(this);

	public InferenceConfigCreateDescriptor() : base()
	{
	}

	private bool ContainsVariant { get; set; }
	private string ContainedVariantName { get; set; }
	private object Variant { get; set; }
	private Descriptor Descriptor { get; set; }

	private InferenceConfigCreateDescriptor Set<T>(Action<T> descriptorAction, string variantName) where T : Descriptor
	{
		ContainedVariantName = variantName;
		ContainsVariant = true;
		var descriptor = (T)Activator.CreateInstance(typeof(T), true);
		descriptorAction?.Invoke(descriptor);
		Descriptor = descriptor;
		return Self;
	}

	private InferenceConfigCreateDescriptor Set(object variant, string variantName)
	{
		Variant = variant;
		ContainedVariantName = variantName;
		ContainsVariant = true;
		return Self;
	}

	public InferenceConfigCreateDescriptor Classification(ClassificationInferenceOptions classificationInferenceOptions) => Set(classificationInferenceOptions, "classification");
	public InferenceConfigCreateDescriptor Classification(Action<ClassificationInferenceOptionsDescriptor> configure) => Set(configure, "classification");
	public InferenceConfigCreateDescriptor FillMask(FillMaskInferenceOptions fillMaskInferenceOptions) => Set(fillMaskInferenceOptions, "fill_mask");
	public InferenceConfigCreateDescriptor FillMask(Action<FillMaskInferenceOptionsDescriptor> configure) => Set(configure, "fill_mask");
	public InferenceConfigCreateDescriptor Ner(NerInferenceOptions nerInferenceOptions) => Set(nerInferenceOptions, "ner");
	public InferenceConfigCreateDescriptor Ner(Action<NerInferenceOptionsDescriptor> configure) => Set(configure, "ner");
	public InferenceConfigCreateDescriptor PassThrough(PassThroughInferenceOptions passThroughInferenceOptions) => Set(passThroughInferenceOptions, "pass_through");
	public InferenceConfigCreateDescriptor PassThrough(Action<PassThroughInferenceOptionsDescriptor> configure) => Set(configure, "pass_through");
	public InferenceConfigCreateDescriptor QuestionAnswering(QuestionAnsweringInferenceOptions questionAnsweringInferenceOptions) => Set(questionAnsweringInferenceOptions, "question_answering");
	public InferenceConfigCreateDescriptor QuestionAnswering(Action<QuestionAnsweringInferenceOptionsDescriptor> configure) => Set(configure, "question_answering");
	public InferenceConfigCreateDescriptor Regression(RegressionInferenceOptions regressionInferenceOptions) => Set(regressionInferenceOptions, "regression");
	public InferenceConfigCreateDescriptor Regression(Action<RegressionInferenceOptionsDescriptor> configure) => Set(configure, "regression");
	public InferenceConfigCreateDescriptor Regression<TDocument>(Action<RegressionInferenceOptionsDescriptor<TDocument>> configure) => Set(configure, "regression");
	public InferenceConfigCreateDescriptor TextClassification(TextClassificationInferenceOptions textClassificationInferenceOptions) => Set(textClassificationInferenceOptions, "text_classification");
	public InferenceConfigCreateDescriptor TextClassification(Action<TextClassificationInferenceOptionsDescriptor> configure) => Set(configure, "text_classification");
	public InferenceConfigCreateDescriptor TextEmbedding(TextEmbeddingInferenceOptions textEmbeddingInferenceOptions) => Set(textEmbeddingInferenceOptions, "text_embedding");
	public InferenceConfigCreateDescriptor TextEmbedding(Action<TextEmbeddingInferenceOptionsDescriptor> configure) => Set(configure, "text_embedding");
	public InferenceConfigCreateDescriptor TextExpansion(TextExpansionInferenceOptions textExpansionInferenceOptions) => Set(textExpansionInferenceOptions, "text_expansion");
	public InferenceConfigCreateDescriptor TextExpansion(Action<TextExpansionInferenceOptionsDescriptor> configure) => Set(configure, "text_expansion");
	public InferenceConfigCreateDescriptor ZeroShotClassification(ZeroShotClassificationInferenceOptions zeroShotClassificationInferenceOptions) => Set(zeroShotClassificationInferenceOptions, "zero_shot_classification");
	public InferenceConfigCreateDescriptor ZeroShotClassification(Action<ZeroShotClassificationInferenceOptionsDescriptor> configure) => Set(configure, "zero_shot_classification");

	protected override void Serialize(Utf8JsonWriter writer, JsonSerializerOptions options, IElasticsearchClientSettings settings)
	{
		if (!ContainsVariant)
		{
			writer.WriteNullValue();
			return;
		}

		writer.WriteStartObject();
		writer.WritePropertyName(ContainedVariantName);
		if (Variant is not null)
		{
			JsonSerializer.Serialize(writer, Variant, Variant.GetType(), options);
			writer.WriteEndObject();
			return;
		}

		JsonSerializer.Serialize(writer, Descriptor, Descriptor.GetType(), options);
		writer.WriteEndObject();
	}
}